/**
 * Coordinate conversion stuff ripped from:
 * http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html
 */
package com.kontza.LocationLogger;

import java.util.Date;
import java.util.Calendar;
import javax.microedition.location.LocationProvider;

class Utils
{
    /**
     * Const for one second in milliseconds.
     */
    public static final int ONE_SECOND = 1000;
    /**
     * Constant for converting speed from m/s into km/h.
     */
    private static final double MPS_INTO_KPH = 3.6;
    /**
     * Array of time field values for efficient
     * generation of ISO 8601 string.
     */
    private static final int timeFields[] =
        {
            Calendar.YEAR,
            Calendar.MONTH,
            Calendar.DAY_OF_MONTH,
            Calendar.HOUR_OF_DAY,
            Calendar.MINUTE,
            Calendar.SECOND
        };
    /**
     * Separators for ISO 8601 string generation.
     */
    private static final String iso8601Separators[] = { "-", "-", "T", ":", ":", "." };
    /**
     * A calendar instance that is used for ISO-8601 print formatting.
     */
    private static final Calendar calNow = Calendar.getInstance();

    /**
     * Convert location status into a string.
     * @param locationProviderState The current status of location provider.
     * @return Input data converted into a string format.
     */
    public static String getStateString( final int locationProviderState )
    {
        String str;
        switch( locationProviderState )
        {
            case LocationProvider.AVAILABLE:
                str = "stable fix";
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                str = "temporarily lost fix";
                break;
            case LocationProvider.OUT_OF_SERVICE:
                str = "out of service";
                break;
            default:
                str = "location unknown";
        }
        return str;
    }

    /**
     * Convert meters/second into km/h.
     */
    public static double calculateSpeedInKph( double dashDistance, double milliseconds )
    {
        return MPS_INTO_KPH * dashDistance / ( milliseconds / ONE_SECOND );
    }

    /**
     * Convert a calendar value into ISO 8601 formatted string:
     * e.g. "2009-03-01T10:28:05.70".
     */
    public static String calendarToISO8601( Calendar calendarToUse )
    {
        //! If calendar to use is null, use our own.
        if( calendarToUse == null )
        {
            calNow.setTime( new Date( System.currentTimeMillis() ) );
        }
        else
        {
            calNow.setTime( calendarToUse.getTime() );
        }
        StringBuffer timeString = new StringBuffer();
        int timefieldData = 0;
        //! Processing loop for time fields.
        for( int i = 0; i < timeFields.length; i++ )
        {
            //! Get the field's value from Calendar object.
            timefieldData = calNow.get( timeFields[i] );
            //! Special case for MONTH.
            if( timeFields[i] == Calendar.MONTH )
            {
                //! Month is zero based, fix it.
                timefieldData++;
            }
            if( timefieldData < 10 )
            {
                //! Add leading zeros.
                timeString.append( "0" );
            }
            /**
             * Append current field value and the corresponding separator
             * into the output string.
             */
            timeString.append( timefieldData );
            timeString.append( iso8601Separators[i] );
        }
        //! Finally, add fractions of a second as in 1/100 s granularity.
        timefieldData = calNow.get( Calendar.MILLISECOND ) / 10;
        if( timefieldData < 10 )
        {
            //! Add leading zeros.
            timeString.append( "0" );
        }
        timeString.append( timefieldData );

        //! Done.
        return timeString.toString();
    }
}
